/********************************** (C) COPYRIGHT *******************************
* File Name          : main.c
* Author             : WCH
* Version            : V1.0.0
* Date               : 2022/08/20
* Description        : Main program body.
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* SPDX-License-Identifier: Apache-2.0
*******************************************************************************/

/* @Note
 * Compatibility HID Example:
 * This program provides examples of the pass-through of USB-HID data and serial port
 *  data based on compatibility HID device. And the data returned by Get_Report request is
 *  the data sent by the last Set_Report request.Speed of UART1/2 is 115200bps.
 *
 * Interrupt Transfers:
 *   UART2_RX   ---> Endpoint1
 *   Endpoint2  ---> UART2_TX
 *
 *   Note that the first byte is the valid data length and the remaining bytes are
 *   the transmission data for interrupt Transfers.
 *
 * Control Transfers:
 *   Set_Report ---> UART1_TX
 *   Get_Report <--- last Set_Report packet
 *
 *  */

#include "ch32v30x_usbhs_device.h"
#include "usbd_compatibility_hid.h"
#include "camera_udp.h"
#include "wchnet.h"
#include "eth_driver.h"
#include "DHT11.h"
/******************************************************************************/
/* Global define */

/******************************************************************************/
/* Global Variables */
u8 DHT11_flag = 0;
u8 temperature, humidity;
int dhtTimer = 0;
/*********************************************************************
 * @fn      Var_Init
 *
 * @brief   Software parameter initialization
 *
 * @return  none
 */
void Var_Init(void)
{
    uint16_t i;
    RingBuffer_Comm.LoadPtr = 0;
    RingBuffer_Comm.StopFlag = 0;
    RingBuffer_Comm.DealPtr = 0;
    RingBuffer_Comm.RemainPack = 0;
    for(i=0; i<DEF_Ring_Buffer_Max_Blks; i++)
    {
        RingBuffer_Comm.PackLen[i] = 0;
    }
}

/*********************************************************************
 * @fn      TIM2_Init
 *
 * @brief   Initializes TIM2.
 *
 * @return  none
 */
void TIM2_Init(void)
{
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure = { 0 };

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

    TIM_TimeBaseStructure.TIM_Period = SystemCoreClock / 1000000 - 1;
    TIM_TimeBaseStructure.TIM_Prescaler = WCHNETTIMERPERIOD * 1000 - 1;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
    TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);

    TIM_Cmd(TIM2, ENABLE);
    TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
    NVIC_EnableIRQ(TIM2_IRQn);
}
/*********************************************************************
 * @fn      main
 *
 * @brief   Main program.
 *
 * @return  none
 */
int main(void)
{

	Delay_Init();
	USART_Printf_Init(115200);
	printf( "SystemClk:%d\r\n",SystemCoreClock) ;
	printf( "Compatibility HID Running On USBHS Controller\n" );

	 /* Variables init */
    Var_Init();

    /* UART2 init */
    UART2_Init();
    UART2_DMA_Init();

    /* USB20 device init */
    USBHS_RCC_Init( );
    USBHS_Device_Init( ENABLE );

    camera_udp_init();

    /* Timer init */
    TIM2_Init();
    printf("start usbh\n");

    if(DHT11_Init())
    {
    	printf("DTH11 error\n");
    }
    else
    {
    	DHT11_Read_Data(&temperature, &humidity);
    	printf("Temp:%d,Humi:%d\n", temperature, humidity);
    	DHT11_flag = 1;
    	printf("DTH11 success\n");
    }

    while(1)
    {
    	dhtTimer++;
    	if(dhtTimer >= 500000)
    	{
    		dhtTimer = 0;
    		if(DHT11_Read_Data(&temperature, &humidity) == 0 && DHT11_flag == 1)
    		{
    			printf("Temp:%d,Humi:%d\n", temperature, humidity);
    			UART2_Rx_Service();
    		}
    	}
    	camera_udp_task();
        if (USBHS_DevEnumStatus)
        {
            UART2_Tx_Service(&dhtTimer);
            HID_Set_Report_Deal();
        }
    }
}
